package leetcode.linked;

import lombok.Data;

/**
 * 合并两个有序链表 简单
 */
public class Solution21合并两个有序链表 extends ListNode {
    //1 --> 3 --> 5 --> 7
    //1 --> 2 --> 6 --> 9

    //1 --> 1 --> 2 --> 3 --> 5 --> 6
    public ListNode mergeTwoListsRecursion(ListNode l1, ListNode l2) {
        if (l1 == null) {
            return l2;
        } else if (l2 == null) {
            return l1;
        } else if (l1.val < l2.val) {
            l1.next = mergeTwoListsRecursion(l1.next, l2);
            return l1;
        } else {
            l2.next = mergeTwoListsRecursion(l1, l2.next);
            return l2;
        }
    }

    //1 --> 4 --> 5
    //1 --> 2 --> 3 --> 6

    //1 --> 1 --> 2 --> 3 --> 4 --> 5 --> 6
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode prehead = new ListNode(-1);

        ListNode prev = prehead;
        while (l1 != null && l2 != null) {
            if (l1.val <= l2.val) {
                prev.next = l1;
                l1 = l1.next;
            } else {
                prev.next = l2;
                l2 = l2.next;
            }
            prev = prev.next;
        }

        // 合并后 l1 和 l2 最多只有一个还未被合并完，我们直接将链表末尾指向未合并完的链表即可
        prev.next = l1 == null ? l2 : l1;

        return prehead.next;
    }

    public static void main(String[] args) {
        ListNode listNode1 = new ListNode(1);

        ListNode listNode2 = new ListNode(4);
        listNode1.next = listNode2;

        listNode2.next = new ListNode(5, null);


        ListNode listNode12 = new ListNode(1);

        ListNode listNode22 = new ListNode(2);
        listNode12.next = listNode22;

        ListNode listNode32 = new ListNode(3);
        listNode22.next = listNode32;

        listNode32.next = new ListNode(6, null);

        Solution21合并两个有序链表 solution21 = new Solution21合并两个有序链表();
        solution21.mergeTwoLists(listNode1, listNode12);
    }

    @Data
    static class Student {
        private Integer id;
        private String name;
    }

    public static void main2(String[] args) {
        Student a = new Student();
        a.setId(1);
        a.setName("张三");

        Student b = a;
        b.setName("李四");
        System.out.println(a == b); //true

        Student c = new Student();
        c.setId(1);
        c.setName("李四");
        b = c;
        System.out.println(a == b); //false
    }
}
